package com.bawei.mvvmdemo.net;



import com.bawei.mvvmdemo.entity.BaseResponseEntity;

import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import androidx.lifecycle.LiveData;
import retrofit2.CallAdapter;
import retrofit2.Retrofit;

/**
 * @Author : yaotianxue
 * @Time : On 2023/9/12 11:05
 * @Description : LiveDataCallAdapterFactory
 */
public class LiveDataCallAdapterFactory extends CallAdapter.Factory {
    //returnType：LiveData<BaseResponseEntity<BannerEntity>>
    @Override
    public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
        //判断原始数据类型是否为LiveData类型
        if (getRawType(returnType) != LiveData.class) {
        }
        Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);
        Type rawType = getRawType(observableType);
        if (rawType != BaseResponseEntity.class) {
            throw new IllegalArgumentException("type must be ApiResponse");
        }
        if (!ParameterizedType.class.isInstance(observableType)) {
            throw new IllegalArgumentException("resource must be Parameterized");
        }
        return new LiveDataCallAdapter<>(observableType);
    }

}
